<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>Document</title>
   <link rel="shortcut icon" href="/resource/fav.ico" type="image/x-icon">
   <link rel="stylesheet" href="/lib/blog-pc.css" media="all and (min-width:901px)">
   <link rel="stylesheet" href="/lib/blog-mobile.css" media="all and (max-width:900px)">
   <link rel="stylesheet" href="/lib/highlight.min.css">
   <link rel="stylesheet" href="/lib/highlight-oceanicnext.css">
   <link rel="stylesheet" href="/lib/jquery.fancybox.min.css">
   <script src="/lib/jquery.min.js"></script>
   <script src="/lib/jquery.fancybox.min.js"></script>
</head>
<body>
    <div id="content-container"></div>
    <div id="page-nav"></div>
    <div id="site-nav"></div>
    <div id="page-nav-shower" onclick="pageNavShow()" class="divHide">大纲</div>
    <div id="site-nav-shower" onclick="siteNavShow()" class="divHide">目录</div>
<script>
    const isMobile = document.body.clientWidth < 800
    if (isMobile) {
        pageNavHide()
        siteNavHide()
    }
    
    // 加载主题目录
    fetch('SUMMARY.html')
    .then(resp => {
        if (!resp.ok)  return
        return resp.text()
    }).then(txt => {
        setTitle(txt)
        txt = '<div id="site-nav-hider" onclick="siteNavHide()">隐藏</div>\n' + txt
        var cc = document.querySelector('#site-nav')
        cc.innerHTML = txt
    })
    function setTitle(txt) {
        try{
            var reg = /<h\d[^<]*>(.+)<\/h\d>/
            var tmp = reg.exec(txt)
            document.title = tmp[1]
        }catch{
            console.log('failed to set title')
        }
    }
    
    // 首次加载
    loadPageContent_Nav('README')
    
    function gotoArch() {
        if (isMobile) {
            pageNavHide()
        }
    }

    function gotoPage(pageNum, elem) {
        gotoTop()

        if (isMobile) {
            siteNavHide()
        }
        
        var nextP = ''
        var base = location.pathname
        if (base.endsWith('/')) {
            nextP = base + pageNum
        } else {
            nextP = base + '/' + pageNum
        }
        loadPageContent_Nav(nextP)

        // 当前目录高亮
        var items = document.querySelectorAll("#site-nav span");
        items.forEach(function(item) {
            item.setAttribute('class', '')
        });
        elem.setAttribute('class', 'current')
    }

    const copyDiv = '<div class="copyer" onclick="toClipboard(this)" title="复制"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></div>'
    function loadPageContent_Nav(nextPage) {
        location.hash = ''
        // load page content
        fetch(nextPage+'.html')
        .then(resp => {
            if (!resp.ok)  return
            return resp.text()
        }).then(txt => {
            var cc = document.querySelector('#content-container')
            if (isMobile) {
                cc.innerHTML = txt
                return
            }

            $(cc).ready(function() {
                $('pre code.hljs').parent('pre').hover(function() {
                    $(this).append(copyDiv)
                }, function() {
                    $('pre .copyer').remove()
                })
            })
            cc.innerHTML = txt
        })
        // load nav
        fetch(nextPage+"_title.html")
        .then(resp => {
            if (!resp.ok)  return
            return resp.text()
        }).then(txt => {
            txt = '<div id="page-nav-hider" onclick="pageNavHide()">隐藏</div>\n' + txt
            var cc = document.querySelector('#page-nav')
            cc.innerHTML = txt
        })
    }
    // 返回页面顶部
    function gotoTop() {
        document.body.scrollTop = 0;
        document.documentElement.scrollTop = 0;
    }
    function toClipboard(elem) {
        // 复制代码至粘贴板
        const txt = $(elem).siblings('code').text()
        navigator.clipboard.writeText(txt).then(function() {
            successTip('内容已复制至粘贴板')
        }, function() {
            failTip('内容复制失败')
        });
    }

    // 提示
    function successTip(msg) {
        showTips(msg, true)
    }
    function failTip(msg) {
        showTips(msg, false)
    }
    var tipIndex = 2000
    function showTips(msg, flag) {
        var divId = 'tipBoard'+tipIndex
        var colorVal = flag ? 'green' : 'tomato'
        var content = `<div class="tip-board" id="${divId}" style="z-index: ${tipIndex}">
                    <div class="tip-board-head" style="background-color: ${colorVal}"></div>
                    <div class="tip-board-content">${msg}</div>
                    </div>`
        tipIndex++
        
        $('body').ready(function() {
            setTimeout(() => {
                $('#'+divId).fadeOut("slow")
            }, 666)
            setTimeout(() => {
                $('#'+divId).remove()
            }, 1200)
        })
        $('body').append(content)
    }

    function siteNavHide() {
        var nav = document.querySelector('#site-nav')
        var navShower = document.querySelector('#site-nav-shower')
        nav.setAttribute('class', 'divHide')
        navShower.setAttribute('class', 'divShow')
        if (isMobile) {
            var navShower = document.querySelector('#page-nav-shower')
            navShower.setAttribute('class', 'divShow')
        }
    }
    function siteNavShow() {
        var nav = document.querySelector('#site-nav')
        var navShower = document.querySelector('#site-nav-shower')
        nav.setAttribute('class', 'divShow')
        navShower.setAttribute('class', 'divHide')
        if (isMobile) {
            var navShower = document.querySelector('#page-nav-shower')
            navShower.setAttribute('class', 'divHide')
        }
    }
    function pageNavHide() {
        var nav = document.querySelector('#page-nav')
        var navShower = document.querySelector('#page-nav-shower')
        nav.setAttribute('class', 'divHide')
        navShower.setAttribute('class', 'divShow')
        if (isMobile) {
            var navShower = document.querySelector('#site-nav-shower')
            navShower.setAttribute('class', 'divShow')
        }
    }
    function pageNavShow() {
        var nav = document.querySelector('#page-nav')
        var navShower = document.querySelector('#page-nav-shower')
        nav.setAttribute('class', 'divShow')
        navShower.setAttribute('class', 'divHide')

        if (isMobile) {
            var navShower = document.querySelector('#site-nav-shower')
            navShower.setAttribute('class', 'divHide')
        }
    }
</script>
</body>
</html>